﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class Inventory_MaterialList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Panel1.Visible = false;
            this.ddlBrand.Items.Add("未知");
            foreach (Brand b in BaseInfoDB.GetBrands())
            {
                this.ddlBrand.Items.Add(new ListItem(b.BrandName, b.BrandID + "|"));
            }
            this.ddlRegion.Items.Add("未知");
            foreach (Region R in BaseInfoDB.GetRegionsExceptHQ())
            {
                this.ddlRegion.Items.Add(new ListItem(R.RegionName, R.RegionID + "|"));
            }
        }
    }
    protected void lbtnNewOrderCycle_Click(object sender, EventArgs e)
    {
        this.txtNewOrderCycle.Text = string.Empty;
        this.txtNewOrderCycle.Focus();
    }

    protected void lbtnSaveOrderCycle_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtNewOrderCycle.Text))
        {
            ListItem item = this.ddlOrderCycle.Items.FindByText(txtNewOrderCycle.Text);
            if (item == null)
            {
                this.ddlOrderCycle.Items.Add(txtNewOrderCycle.Text);
                this.ddlOrderCycle.SelectedIndex = this.ddlOrderCycle.Items.Count - 1;
            }
            else
            {

                item.Selected = true;
            }
        }
    }
    protected void lbtnNewStocktakingCycle_Click(object sender, EventArgs e)
    {
        this.txtNewStocktakingCycle.Text = string.Empty;
        this.txtNewStocktakingCycle.Focus();
    }
    protected void lbtnSaveStocktakingCycle_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtNewStocktakingCycle.Text))
        {
            ListItem item = this.ddlStocktakingCycle.Items.FindByText(txtNewStocktakingCycle.Text);
            if (item == null)
            {
                this.ddlStocktakingCycle.Items.Add(txtNewStocktakingCycle.Text);
                this.ddlStocktakingCycle.SelectedIndex = this.ddlStocktakingCycle.Items.Count - 1;
            }
            else
            {

                item.Selected = true;
            }
        }
    }
    protected void lbtnSaveDeliveryType_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtNewDeliveryType.Text))
        {
            ListItem item = this.ddlDeliveryType.Items.FindByText(txtNewDeliveryType.Text);
            if (item == null)
            {
                this.ddlDeliveryType.Items.Add(txtNewDeliveryType.Text);
                this.ddlDeliveryType.SelectedIndex = this.ddlDeliveryType.Items.Count - 1;
            }
            else
            {

                item.Selected = true;
            }
        }
    }
    protected void lbtnDeliveryType_Click(object sender, EventArgs e)
    {
        this.txtNewDeliveryType.Text = string.Empty;
        this.txtNewDeliveryType.Focus();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.Panel1.Visible = true;
        this.MultiView1.ActiveViewIndex = 0;
        this.MultiView2.ActiveViewIndex = 0;
        this.MultiView3.ActiveViewIndex = 0;
        string MaterialID = this.GridView1.SelectedDataKey.Value.ToString();
        foreach (ListItem item in this.chkBrandList.Items)
        {
            item.Selected = false;
        }
        foreach (ListItem item in this.chkRegionList.Items)
        {
            item.Selected = false;
        }
        this.Panel1.DataBind();
        Material material = Inventory.getMaterialsByMaterialID(MaterialID);
        this.Panel1.GroupingText = "设置【" + MaterialID + "--" + material.MaterialName + "】属性";
        foreach (string s in material.ApplyBrand)
        {
            if (!string.IsNullOrEmpty(s.Trim()))
            {
                ListItem item = chkBrandList.Items.FindByValue(s);
                if (item != null)
                {
                    item.Selected = true;
                }
            }
        }
        foreach (string s1 in material.ApplyRegion)
        {
            if (!string.IsNullOrEmpty(s1.Trim()))
            {
                ListItem item1 = chkRegionList.Items.FindByValue(s1);
                if (item1 != null)
                {
                    item1.Selected = true;
                }
            }
        }
        this.txtMinOrderValue.Text = material.MinOrderValue.ToString();
        this.txtMaxOrderValue.Text = material.MaxOrderValue.ToString();

        this.ddlOrderCycle.Text = material.Order_Cycle;
        this.ddlStocktakingCycle.Text = material.Stocktaking_Cycle;
        this.ddlDeliveryType.Text = material.DeliveryType;


    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        this.Panel1.Visible = false;
        this.GridView1.SelectedIndex = -1;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        List<string> list = new List<string>();
        string Brands = string.Empty;
        string Regions = string.Empty;
        foreach (ListItem item in this.chkBrandList.Items)
        {
            if (item.Selected)
            {
                list.Add(item.Value);
            }
        }
        if (list.Count > 0)
        {
            Brands = string.Join("|", list.ToArray()) + "|";

        }
        list.Clear();
        foreach (ListItem item in this.chkRegionList.Items)
        {
            if (item.Selected)
            {
                list.Add(item.Value);
            }
        }
        if (list.Count > 0)
        {
            Regions = string.Join("|", list.ToArray()) + "|";

        }
        list.Clear();
        if (string.IsNullOrEmpty(Brands) || string.IsNullOrEmpty(Regions))
        {
            lblMessage.Text = "保存失败，因适用品牌或适用地区为空";
        }
        else
        {
            bool b = Inventory.UpdateMaterialBaseInfo(this.GridView1.SelectedDataKey.Value.ToString(), string.IsNullOrEmpty(txtMinOrderValue.Text) ? 0 : float.Parse(txtMinOrderValue.Text), string.IsNullOrEmpty(txtMaxOrderValue.Text) ? 0 : float.Parse(txtMaxOrderValue.Text), ddlOrderCycle.Text, ddlStocktakingCycle.Text, ddlDeliveryType.Text, Brands, Regions, txtDescription.Text);
            if (b)
            {
                Panel1.Visible = false;
            }
            else
            {
                lblMessage.Text = "保存失败";
            }
        }
        this.GridView1.DataBind();
        this.chkBrandAll.Checked = false;
        this.chkRegionAll.Checked = false;

    }
    protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {
        this.Panel1.Visible = false;
        this.GridView1.SelectedIndex = -1;
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "ChangeStatus")
        {
            string status = string.Empty;
            LinkButton lbtn = (LinkButton)e.CommandSource;
            if (lbtn.Text.Equals("设为禁用"))
            {
                status = "禁用";
            }
            else
            {
                status = "启用";
            }
            Inventory.ChangedMaterialStatus(e.CommandArgument.ToString(), status);
            this.GridView1.DataBind();
            this.ddlStatus.DataBind();
        }
    }
    protected void chkBrandAll_CheckedChanged(object sender, EventArgs e)
    {
        foreach (ListItem item in chkBrandList.Items)
        {
            item.Selected = chkBrandAll.Checked;
        }
    }
    protected void chkRegionAll_CheckedChanged(object sender, EventArgs e)
    {
        foreach (ListItem item in chkRegionList.Items)
        {
            item.Selected =chkRegionAll.Checked;
        }
    }
}
